import Api from "@/libs/api";
import Cache from "@/libs/cache";
import Store from "@/libs/store";
import Tool from "@/libs/tool";

let user = {
    id: 0,
    member_level: 1,
    username: "",
    email: "",
    status: 100,
    created_at: 0,
    signup_message: "",
    updated_at: 0,
    nickname: null,
    avatar: "",
    weixin_exceptional_code: "",
    weixin_exceptional_url: "",
    alipay_exceptional_code: "",
    alipay_exceptional_url: "",
    mobile: null,
    qq: null,
    weixin: null,
    _created_at: "2022-12-25 11:57:54",
    __isYour: false,
    _isYour: 0,
    _isYourLeader: 0,
    _isYourFans: 0,
    _leaderTotal: 0,
    _fansTotal: 0,
    _publishTotal: 0,
    _collectTotal: 0,
    _buyTotal: 0,
    is_admin: 0, // admin
    token: "", // admin
    key: "", // admin
    balance: 0, // admin
    frozen: 0, // admin
    cannot_withdrawn: 0, // admin
}
let User = {
    toProfile(id) {
        uni.navigateTo({
            url: '/pages/user/profile/Profile?id=' + id
        })
    },
    toggleAttention(item) {
        let _this = this;
        return new Promise((resolve, reject) => {
            if (item._isYourLeader > 0) {
                uni.showModal({
                    title: '提示',
                    content: '确定取消关注吗？',
                    success: function (res) {
                        if (res.confirm) {
                            Api.post('/user/profile/un-attention', {
                                id: item.id
                            }).then(async respData => {
                                if (respData.status === 200) {
                                    item._isYourLeader = 0;
                                    let profile = await _this.getOrSetProfileCacheById(item.id, Tool.getTimestamp());
                                    uni.$u.toast("取消关注成功");
                                    resolve(profile);
                                } else {
                                    uni.$u.toast("取消关注失败");
                                    reject();
                                }
                            });
                        }
                    }
                });
            } else {
                uni.showModal({
                    title: '提示',
                    content: '确定关注吗？',
                    success: function (res) {
                        if (res.confirm) {
                            Api.post('/user/profile/attention', {
                                id: item.id
                            }).then(async respData => {
                                if (respData.status === 200) {
                                    item._isYourLeader = 1;
                                    let profile = await _this.getOrSetProfileCacheById(item.id, Tool.getTimestamp());
                                    uni.$u.toast("关注成功");
                                    resolve(profile);
                                } else {
                                    uni.$u.toast("关注失败");
                                    reject();
                                }
                            });
                        }
                    }
                });
            }
        });
    },
    getInitData() {
        return user;
    },
    getIdCacheKey(id) {
        return "Profile:" + id;
    },
    getProfileCacheById(id) {
        let _this = this;
        let cacheModel =  Cache.get(_this.getIdCacheKey(id));
        if (!cacheModel) return null;
        cacheModel.__isYour = cacheModel.id === Store.getters['Auth/getUserInfo'].id;
        return cacheModel;
    },
    setProfileCacheById(id, cacheModel) {
        let _this = this;
        Cache.set(_this.getIdCacheKey(id), cacheModel);
    },
    deleteProfileCacheById(id) {
        let _this = this;
        Cache.delete(_this.getIdCacheKey(id));
    },
    async getOrSetProfileCacheById(id, updated_at = 0) {
        let _this = this;
        let cacheModel = _this.getProfileCacheById(id);
        // 是否需要更新
        let bool = !cacheModel || updated_at > cacheModel.updated_at;
        if (bool) {
            let res = await Api.post('/user/profile/index', {id, updated_at});
            _this.resetProfileCacheForId(id, res.data);
            cacheModel = _this.getProfileCacheById(id);
        }
        return cacheModel;
    },
    resetProfileCacheForId(id, cacheModel) {
        let _this = this;
        _this.deleteProfileCacheById(id);
        _this.setProfileCacheById(id, cacheModel);
    }
}
export default User
